package cn.com.generaldata.os.service.indicator_evaluation.composition_analysis;

import java.math.BigDecimal;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.pojo.IndicatorEvaluationResult;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 海洋生产总值占上级海洋生产总值比重评估指标计算类.
 */
public class GOPtoGOPCalculateService extends IndicatorEvaluationCalculateService {

	private String GOPIndicatorName = ProvinceIndicatorConstant.GOP_INDICATOR_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateResult(indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			if ("320900000000000".equals(indicatorAccountVO.getAreaId())) {
				calculateOtherCityResult(indicator, indicatorAccountVO);
			}
			result = calculateResult(indicatorAccountVO);
		} else {
			result = calculateResult(indicatorAccountVO);
		}
		return result;
	}

	@SuppressWarnings("null")
	private void calculateOtherCityResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		// Double cityGOP = getCityGOP(indicatorAccountVO, indicatorAccountVO.getYear());
		Double result;
		Double provinceGOP = getParentGOP(indicatorAccountVO, indicatorAccountVO.getYear());
		Validate.isTrue(provinceGOP != null);

		Double cityGOPCount = 0d;
		for (String cityId : ProvinceIndicatorConstant.CITYS) {
			IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
			vo.setAreaId(cityId);
			cityGOPCount += getGOP(vo, indicatorAccountVO.getYear());
		}

		result = ((provinceGOP - cityGOPCount) / provinceGOP) * 100;

		IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		vo.setAreaId(ProvinceIndicatorConstant.OTHER_CITY_AREA_ID);

		result = new BigDecimal(result).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		IndicatorEvaluationResult ier = createIndicatorEvaluationResult(indicator, vo);
		ier.setIndicatorValue(result);
		Criteria cri = getIndicatorAccountingDataCri(indicator, vo);

		Record record = dao.fetch("tbl_indicator_evaluation_res", cri);
		if (record != null) {
			dao.clear("tbl_indicator_evaluation_res", cri);
		}

		saveIndicatorEvaluationResult(ier);
	}

	@SuppressWarnings("null")
	private Double calculateResult(IndicatorAccountVO indicatorAccountVO) {
		Double GOP = getGOP(indicatorAccountVO, indicatorAccountVO.getYear());
		Validate.isTrue(GOP != null);

		Double parentGOP = getParentGOP(indicatorAccountVO, indicatorAccountVO.getYear());
		Validate.isTrue(parentGOP != null);

		return (GOP / parentGOP) * 100;
	}

	private Double getParentGOP(IndicatorAccountVO indicatorAccountVO, int year) {
		String parentAreaId = getAreaParentId(indicatorAccountVO.getAreaId());
		IndicatorAccountVO provinceVO = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		provinceVO.setAreaId(parentAreaId);
		return getInputOrAccountingIndicatorValue(provinceVO, year, GOPIndicatorName);
	}

	private Double getGOP(IndicatorAccountVO indicatorAccountVO, int year) {
		return getInputOrAccountingIndicatorValue(indicatorAccountVO, year, GOPIndicatorName);
	}

}
